package com.qh.pay.api;

import com.alibaba.fastjson.JSONObject;
import com.qh.common.utils.LocalDateTimeUtil;
import com.qh.pay.api.utils.ParamUtil;
import com.qh.pay.domain.PayQrConfigDO;
import org.apache.http.client.fluent.Request;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @ClassName PayApi
 * @Description TODO
 * @Author keph@guahao.com
 * @Date 2019/9/12 11:08
 * @Version 1.0
 */
public class PayApi {

public static final  String apikey = "ksgo4IveLyxiJUdcjsaeVPmSfh5Vj5od";

    public static int pay(PayQrConfigDO payQrConfig) {

        String resutl = null;
        try {
            resutl = Request.Get("http://api.tbk.dingdanxia.com/shop/order?apikey="+apikey +
                    "&nick=" + payQrConfig.getAccountName() + "&order_no=" +  payQrConfig.getAccountNo() )
                    .execute().returnContent().asString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ParamUtil.isNotEmpty(resutl)) {
            if (JSONObject.parseObject(resutl).getIntValue("code") == 200) {
                JSONObject jsonObject = JSONObject.parseObject(resutl).getJSONObject("data");
                if ("WAIT_BUYER_PAY".equals(jsonObject.getString("status")) ) {
                    payQrConfig.setMoney(Double.valueOf(jsonObject.getString("payment")));
                    payQrConfig.setEndDate(LocalDateTimeUtil.convert(jsonObject.getString("created")).plusHours(23));

                    return 1;
                } if ("TRADE_FINISHED".equals(jsonObject.getString("status")) || "SELLER_CONSIGNED_PART".equals(jsonObject.getString(
                        "status")) || "WAIT_SELLER_SEND_GOODS".equals(jsonObject.getString("status"))
                        || "WAIT_BUYER_CONFIRM_GOODS".equals(jsonObject.getString("status"))) {
                    return 2;
                }else{
                    return 3;
                }
            }else{
                return 0;
            }
        }

        return 4;
    }

}
